/*
 * Copyright 2022 MindMotion Microelectronics Co., Ltd.
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "bsp_rtc.h"

RTC_Init_Type rtc_init;
RTC_Time_Type rtc_time;

void BSP_RTC_Init(void)
{
	rtc_init.ClockFreq     =  APP_RTC_CLOCKFREQ;
    rtc_init.CountFreq     =  APP_RTC_COUNTFREQ;

    /* Setup RTC clock initialization time. */
    rtc_time.Years         =  APP_RTC_INIT_YEARS;
    rtc_time.Months        =  APP_RTC_INIT_MONTHS;
    rtc_time.Days          =  APP_RTC_INIT_DAYS;
    rtc_time.Hours         =  APP_RTC_INIT_HOURS;
    rtc_time.Mins          =  APP_RTC_INIT_MINUTES;
    rtc_time.Secs          =  APP_RTC_INIT_SECONDS;
    rtc_time.Lyears        =  APP_RTC_LOWEST_YEARS;

    //while ( 0x00 == (RTC_GetStatus(BOARD_RTC_PORT) & RTC_STATUS_SYNC) ) /* Wait for register synchronization. */
    {
    }

    RTC_Init(BOARD_RTC_PORT, &rtc_init);    /* Initialize RTC. */
    //while ( 0x00 == (RTC_GetStatus(BOARD_RTC_PORT) & RTC_STATUS_OPERATION)  )  /* Wait for write operation finish. */
    {
    }

    /* Wait for register synchronization. Only register synchronization can read RTC register. */
    //while ( 0u == (RTC_GetStatus(BOARD_RTC_PORT) & RTC_STATUS_SYNC) )
    {
    }
    RTC_SetTime(BOARD_RTC_PORT, &rtc_time);
    //while ( 0u == (RTC_GetStatus(BOARD_RTC_PORT) & RTC_STATUS_OPERATION)  )  /* Wait for write operation finish. */
    {
    }
}

/* Get current time of RTC. */
void BSP_RTC_GetTime(void)
{
    rtc_time.Lyears  = APP_RTC_LOWEST_YEARS;
    //while ( 0u == (RTC_GetStatus(BOARD_RTC_PORT) & RTC_STATUS_SYNC) )  /* Wait to register synchronization. Only register synchronization can read RTC register. */
    {
    }
    RTC_CalcTime(BOARD_RTC_PORT, &rtc_time);
}

/* EOF. */
